﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using System.Data;
using Gift.Model;
using Gift.DAL;
using Gift.Utile;

namespace WebSite.cn
{
    public partial class account : BasePage
    {
        // Fields
        public string address = "";
        public string carid = "0";
        public string carInfoProfession = "";
        public string carInfoShop = "";
        public decimal professionPrice = 0;
        public decimal shopprice = 0;
        public string cname = "";
        public string email = "";
        public string istrue = "0";
        public string phone = "";
        public string post = "";
        public string score1 = "0";
        public string score2 = "0";
        public string score3 = "0";
        public string totalprice = "0";
       // public string shipmsg = "";
        public string tel = "";
        public string totalscore = "0";
        public string accountmoney = "0";
        public string sendDate = DateTime.Now.AddDays(3).ToString("yyyy-MM-dd");

        public string tempOrderCode = "";

        public string priceMessage = string.Empty;

        readonly MemberAccount theMemberAccount = new MemberAccount();
        readonly Member theMember = new Member();
        readonly OrderAccount theOrderAccount = new OrderAccount();

        string _sOperateNoPayOrder = "<strong>操作</strong>";

        public string SOperateNoPayOrder
        {
            get { return _sOperateNoPayOrder; }
        }

        string _displayOrderForm = "display:block;";

        public string DisplayOrderForm
        {
            get { return _displayOrderForm; }
          
        }

        string _displayPlay = "display:none;";

        public string DisplayPlay
        {
            get { return _displayPlay; }

        }



        // Methods
        private void Bind()
        {  
            DataTable table;
            DicProductPrice = null;
            if (base.LoginUserMember != null)
            {
                table = SQLAccess.GreatDs("select membername,tel1,phone,post,address,email,score1,score2,score3 from DB_Member where memberid=" + base.LoginUserMember.MemberId + "").Tables[0];
                if (table.Rows.Count > 0)
                {
                    this.cname = table.Rows[0]["membername"].ToString();
                    this.tel = table.Rows[0]["tel1"].ToString();
                    this.phone = table.Rows[0]["phone"].ToString();
                    this.post = table.Rows[0]["post"].ToString();
                    this.address = table.Rows[0]["address"].ToString();
                    this.email = table.Rows[0]["email"].ToString();
                    this.score1 = table.Rows[0]["score1"].ToString();
                    this.score2 = table.Rows[0]["score2"].ToString();
                    this.score3 = table.Rows[0]["score3"].ToString();
                  //  this.sendDate = table.Rows[0]["sendDate"].ToString();
                }
            }
            if (base.Request.QueryString["action"] != null)
            {
                if (re.replace(base.Request.QueryString["action"]) == "delorder")
                {
                    SQLAccess.DoSql("update DB_Car set flag = 2 where orderid = " + Request.QueryString["orderid"]);
                    base.Response.Write("<script language='javascript'>alert('取消订单成功!');window.location.href='account.aspx';</script>");
                    base.Response.End();
                }
                else
                {
                    string str3 = re.replace(base.Request.QueryString["carid"]);
                    if (re.replace(base.Request.QueryString["action"]) == "edit")
                    {
                        string str2 = re.replace(base.Request.Form["car_" + str3 + ""]);
                        table = SQLAccess.GreatDs("select carid,pronum,price,score from DB_Car where carid=" + str3 + "").Tables[0];
                        if (table.Rows.Count > 0)
                        {
                            SQLAccess.DoSql("update DB_Car set pronum=" + str2 + ",sumprice=" + Convert.ToString((float)(Convert.ToSingle(str2) * Convert.ToSingle(table.Rows[0]["price"]))) + ",sumscore=" + Convert.ToString((float)(Convert.ToSingle(str2) * Convert.ToSingle(table.Rows[0]["score"]))) + " where carid=" + table.Rows[0]["carid"].ToString() + "");
                            base.Response.Write("<script language='javascript'>alert('修改成功!');window.location.href='account.aspx';</script>");
                            base.Response.End();
                        }
                    }
                    if (re.replace(base.Request.QueryString["action"]) == "del")
                    {
                        SQLAccess.DoSql("delete from DB_Car where carid=" + str3 + "");
                        base.Response.Write("<script language='javascript'>alert('删除成功!');window.location.href='account.aspx';</script>");
                        base.Response.End();
                    }
                }
            
            }
            if (base.LoginUserMember != null)
            {
                table = SQLAccess.GreatDs("select DB_Car.carid,DB_Car.pronum,DB_Car.price,DB_Car.sumprice,DB_Car.sumscore,DB_Car.contents,isnull(DB_Shoppro.proisnew3,0) as proisnew3,DB_Shoppro.typeid,DB_Shoppro.proname,DB_Shoppro.proprice1,DB_Shoppro.propic2,DB_Shoppro.proid,DB_Shoppro.professionAreaTypeId,DB_Car.professionAreaName,isnull(DB_car.orderId,0) as orderId  from DB_Car inner join DB_Shoppro on DB_Car.productid=DB_Shoppro.proid where DB_Car.flag=0 and DB_Car.temp=" + base.LoginUserMember.MemberId + "").Tables[0];
                if (table.Rows.Count > 0)
                {
                    StringBuilder sbProfesstion = new StringBuilder();
                    StringBuilder sbShop = new StringBuilder();

                    this.istrue = "1";
                    StringBuilder sbCarinfo = new StringBuilder();
                    foreach (DataRow row in table.Rows)
                    {
                        sbCarinfo = new StringBuilder();
                        this.carid = this.carid + "," + row["carid"].ToString();

                        this.totalscore = Convert.ToString((uint)(Convert.ToUInt32(row["sumscore"]) + Convert.ToUInt32(this.totalscore)));

                        if (row["orderId"].ToString() != "0" && Request.QueryString["type"] == "topay")
                        {
                            sbCarinfo.Append("<tr>");
                            sbCarinfo.Append("<td align='center'><a href='products_details.aspx?pid=" + row["proid"].ToString() + "'><img src='../uploadfile/product/" + row["propic2"].ToString() + "' align='left' border='0'  width='106' /></a></td>");
                            sbCarinfo.Append("<td align='left' class='article_content'><a href='products_details.aspx?pid=" + row["proid"].ToString() + "' class='blue'>" + row["proname"].ToString() + "</a><br />" + row["contents"].ToString() + "</td>");
                            sbCarinfo.Append("<td align='center'>" + string.Format("{0:f}", row["price"]) + " 元 </td>");
                            sbCarinfo.Append("<td align='center'>" + row["pronum"].ToString() + "</td>");
                            sbCarinfo.Append("<td align='center'>" + string.Format("{0:f}", row["sumprice"]) + " 元 </td>");
                            sbCarinfo.Append("<td align='center'></td>");
                            sbCarinfo.Append("</tr>");

                        }
                        else if(row["orderId"].ToString() == "0")
                        {
                            sbCarinfo.Append("<tr>");
                            sbCarinfo.Append("<td align='center'><a href='products_details.aspx?pid=" + row["proid"].ToString() + "'><img src='../uploadfile/product/" + row["propic2"].ToString() + "' align='left' border='0'  width='106' /></a></td>");
                            sbCarinfo.Append("<td align='left' class='article_content'><a href='products_details.aspx?pid=" + row["proid"].ToString() + "' class='blue'>" + row["proname"].ToString() + "</a><br />" + row["contents"].ToString() + "</td>");
                            sbCarinfo.Append("<td align='center'>" + string.Format("{0:f}", row["price"]) + " 元 </td>");
                            sbCarinfo.Append("<form name='frm" + row["carid"].ToString() + "' action='account.aspx?carid=" + row["carid"].ToString() + "&action=edit' method='post'><td align='center'><input name='car_" + row["carid"].ToString() + "' class='box' size='3' maxlength='5' value='" + row["pronum"].ToString() + "' type='text' />");
                            sbCarinfo.Append("&nbsp;&nbsp;<input type=image src='images/delete2.gif' /></td></form>");
                            sbCarinfo.Append("<td align='center'>" + string.Format("{0:f}", row["sumprice"]) + " 元 </td>");
                            sbCarinfo.Append("<td align='center'><a href=javascript:if(confirm('确实要删除吗?')){window.location.href='account.aspx?carid=" + row["carid"].ToString() + "&action=del';}><img src='images/delete.gif' align='absmiddle' border='0' /></a></td>");
                            sbCarinfo.Append("</tr>");
                        }

                        if (base.LoginUserMember.DicUserCardList != null && base.LoginUserMember.DicUserCardList.Count > 0)
                        {
                            if (row["professionAreaName"].ToString() == "")
                            {
                                if (DicProductPrice == null)
                                {
                                    DicProductPrice = new Dictionary<int, decimal>();
                                }
                                professionPrice = professionPrice + Convert.ToDecimal(row["sumprice"]);
                                DicProductPrice[int.Parse(row["professionAreaTypeId"].ToString())] = DicProductPrice.ContainsKey(int.Parse(row["professionAreaTypeId"].ToString())) ? DicProductPrice[int.Parse(row["professionAreaTypeId"].ToString())] + Convert.ToDecimal(row["sumprice"]) : Convert.ToDecimal(row["sumprice"]);
                                sbProfesstion.Append(sbCarinfo);
                                totalprice = professionPrice.ToString();
                                this.divProfession.Visible = true;
                                this.tempOrderCode = theOrderAccount.GenerateOrderCodeForBefor();
                                this.carInfoProfession = sbProfesstion.ToString();
                            }
                        }else
                        {
                            if (row["professionAreaName"].ToString() == EnumProfessionPriceType.Shopping.ToString() || row["professionAreaName"].ToString() == EnumProfessionPriceType.Card.ToString())
                               {
                                   if ((row["orderId"].ToString() != "0" && Request.QueryString["type"] == "topay")
                                       || row["orderId"].ToString() == "0")
                                   {
                                       shopprice = shopprice + Convert.ToDecimal(row["sumprice"]);
                                       base.ShopProductPrice = shopprice;
                                       sbShop.Append(sbCarinfo);
                                       totalprice = shopprice.ToString();

                                       if (DicProductPrice == null)
                                       {
                                           DicProductPrice = new Dictionary<int, decimal>();
                                       }
                                       DicProductPrice[0] = DicProductPrice.ContainsKey(0) ? DicProductPrice[0] + Convert.ToDecimal(row["sumprice"]) : Convert.ToDecimal(row["sumprice"]);
                               
                                   }
                            }
                            if (row["orderId"].ToString() != "0" && Request.QueryString["type"] == "topay")
                            {
                                this.tempOrderCode = row["orderId"].ToString();
                                _sOperateNoPayOrder = "<a style='cursor:pointer' onclick=\" if( confirm('确定要取消订单吗？')){ window.location.href='account.aspx?action=delorder&orderid=" + this.tempOrderCode + "';}\" ><b>取消订单</b></a>";
                            }
                            else
                            {
                                this.tempOrderCode = theOrderAccount.GenerateOrderCodeForBefor();
                            }
                            this.divShopping.Visible = true;
                            this.divProfession.Visible = false;
                        }

                       
                    }

                    if (table.Rows[0]["orderId"].ToString() != "0" && Request.QueryString["type"] == "topay")
                    {
                        Order theOrder = new Order();
                        dbOrder oOrder = theOrder.GetModel( Int64.Parse(table.Rows[0]["orderId"].ToString()));
                        if (oOrder != null)
                        {
                            this.tempOrderCode = oOrder.ordercode;
                            this.carInfoShop = sbShop.ToString();
                            _displayOrderForm = "display:none;";
                            _displayPlay = "display:block;";
                        }
                    }
                    else
                    {
                        this.carInfoShop = sbShop.ToString();
                    }
                }
                else
                {
                 //   this.carinfo = "<tr><td align='center' colspan='6' height=45>您的购物车中无商品</td></tr>";
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.Bind();
              priceMessage = base.SetProductPrice();

  
            }
        }

        
   

        protected void lbtnExit_Click(object sender, EventArgs e)
        {
            Session.Clear();
            Response.Redirect("index.aspx");
        }

     

    }
}